﻿using System;
using System.Collections.Generic;
using Educational.Model.Entity;
using NHibernate;
using NHibernate.Criterion;

namespace Educational.Model.EntityManager
{
    public class EclassManager
    {
        public static void Add(ISession session, Eclass an)
        {
            session.Save(an);
        }

        public static void Delete(ISession session, Eclass an)
        {
            session.Delete(an);
        }

        public static void Update(ISession session, Eclass an)
        {
            session.Update(an);
        }

        public static Eclass Get(ISession session, int itemId)
        {
            return session.Get<Eclass>(itemId);
        }

        public static Eclass Get1(ISession session, int eclassId, int schoolId)
        {
            ICriteria c = session.CreateCriteria(typeof(Eclass));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            c.Add(Expression.Eq("eclassId", eclassId));
            c.SetFetchMode("leader", FetchMode.Eager);
            c.SetFetchMode("grade", FetchMode.Eager);
            return c.UniqueResult<Eclass>();
        }

        public static IList<Eclass> Gets1(ISession session, int schoolId, int rowIndex, int rowCount)
        {
            ICriteria c = session.CreateCriteria(typeof(Eclass));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            c.SetFetchMode("leader", FetchMode.Eager);
            c.SetFetchMode("grade", FetchMode.Eager);
            c.SetFirstResult(rowIndex);
            c.SetMaxResults(rowCount);
            c.AddOrder(Order.Desc("eclassId"));
            return c.List<Eclass>();
        }
        public static int Gets1Count(ISession session, int schoolId)
        {
            ICriteria c = session.CreateCriteria(typeof(Eclass));
            ICriteria c1 = c.CreateCriteria("school", NHibernate.SqlCommand.JoinType.LeftOuterJoin);
            if (schoolId > 0)
                c1.Add(Expression.Eq("schoolId", schoolId));
            return (int)c.SetProjection(Projections.Count("eclassId")).UniqueResult();
        }
    }
}
